class Solution
{
public:
    int countPairs(vector<int> &deliciousness)
    {
        unordered_map<int, long long> deliciousnessCount;
        for (int d : deliciousness)
        {
            ++deliciousnessCount[d];
        }
        vector<int> powOf2;
        for (int i = 0; i <= 21; ++i)
        {
            powOf2.push_back(1 << i);
        }
        long long result = 0;
        for (auto &[delicious, count] : deliciousnessCount)
        {
            for (auto &totalDelicious : powOf2)
            {
                if (totalDelicious == delicious * 2)
                {
                    result += count * (count - 1LL);
                }
                else if (deliciousnessCount.count(totalDelicious - delicious))
                {
                    result += count * deliciousnessCount[totalDelicious - delicious];
                }
            }
        }
        return (result / 2) % 1000000007;
    }
};